package com.scy.weibotest01.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
public class UploadController {
    @RequestMapping("/upload")
    public String upload(MultipartFile picFile) throws IOException {
        String filename = picFile.getOriginalFilename();
        String suffix = filename.substring(filename.lastIndexOf("."));
        filename = UUID.randomUUID()+suffix;
        String driName = "d:/files";
        String dataPath = new SimpleDateFormat("/yyyy/MM/dd/").format(new Date());
        File filePath = new File(driName + dataPath);
        if (filePath.exists()){
            filePath.mkdir();
        }
        picFile.transferTo(new File(driName+dataPath+filename));
        return dataPath+filename;
    }
    @RequestMapping("/remove")
    public void remove(String url){
        new File("d:/files"+url).delete();
    }

}
